package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.exception.ParamMissException;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工管理服务接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登录接口")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }



    @PostMapping
    public Result insertEmployee(@RequestBody  EmployeeDTO employeeDTO){
        int insertCnt=employeeService.insertEmployee(employeeDTO);
        if (insertCnt>0){
            return Result.success(insertCnt);
        }else
            return Result.error("插入失败");

    }

    @GetMapping("/page")
    public Result<PageResult> findPage(EmployeePageQueryDTO pageQuery){
        PageResult pageResult=employeeService.findPage(pageQuery);
        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    public Result updateStatus(@RequestBody @PathVariable("status") Integer status,Long id) throws ParamMissException {
        int updateSta=employeeService.updateStatus(status,id);
        if (updateSta!= 1){
            return Result.error("状态变更失败");
        }else
            return Result.success(updateSta);
    }

    @GetMapping("/{id}")
    public Result<Employee> findById(@PathVariable("id")Long id) throws ParamMissException {
        Employee employee=employeeService.findById(id);
        return Result.success(employee);
    }

    @PutMapping
    public Result updateEmpById(@RequestBody EmployeeDTO employeeDTO) throws ParamMissException {
        int i=employeeService.updateEmpById(employeeDTO);
        return Result.success(i);
    }

    @PutMapping("/editPassword")
    public Result editPassword(@RequestBody PasswordEditDTO passwordEditDTO){
       passwordEditDTO.setEmpId(BaseContext.getCurrentId());
        int i=employeeService.editPassword(passwordEditDTO);
        System.out.println(i);
        return Result.success(i);
    }
}


